package com.feilong.mscv5plus;

import android.content.Context;
import android.util.Log;

import java.io.ByteArrayOutputStream;

import top.keepempty.sph.library.SerialPortHelper;
import top.keepempty.sph.library.SphCmdEntity;
import top.keepempty.sph.library.SphResultCallback;

/**
 * @ClassName: SerialPortUtil
 * @Description: java类作用描述
 * @Author: potato
 * @CreateDate: 2022/10/14 16:16
 * @UpdateUser: potato
 * @UpdateDate: 2022/10/14 16:16
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SerialPortUtil {
    private static final String TAG = "SerialPortUtil";
    private SerialPortHelper serialPortHelper;

    public SerialPortUtil(Context context) {
        //方式一：快速接入方式，设置好串口地址，或者地址和波特率即可，数据位、停止位、校验类型分别默认为8、1、N。
        serialPortHelper = new SerialPortHelper(32);
//serialPortHelper.openDevice("dev/ttyS0");
        serialPortHelper.openDevice("dev/ttyS5", 115200);
// 数据接收回调
        serialPortHelper.setSphResultCallback(new SphResultCallback() {
            @Override
            public void onSendData(SphCmdEntity sendCom) {
                Log.d(TAG, "发送命令：" + sendCom.commandsHex);
                Log.d(TAG, "发送命令：" + decode(sendCom.commandsHex));
                if(null!=onPortListener){
//                    onPortListener.onSend(decode(sendCom.commandsHex));
                }
            }

            @Override
            public void onReceiveData(SphCmdEntity data) {
                Log.d(TAG, "收到命令：" + data.commandsHex);

            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete：");
                if(null!=onPortListener) {
//                    onPortListener.onSend("onComplete");
                }
            }
        });
    }
    public void send(String text){
        Log.d(TAG, "send: "+toHex(text));
        serialPortHelper.addCommands(toHex(text));

    }
    public boolean isOpen(){
        return serialPortHelper.isOpenDevice();
    }
    public String toHex(String str) {
        String hexString="0123456789ABCDEF";
        byte[] bytes=str.getBytes();
        StringBuilder hex=new StringBuilder(bytes.length * 2);
        for(int i=0;i<bytes.length;i++) {
            hex.append(hexString.charAt((bytes[i] & 0xf0) >> 4));  // 作用同 n / 16
            hex.append(hexString.charAt((bytes[i] & 0x0f) >> 0));  // 作用同 n
//            hex.append(' ');  //中间用空格隔开
        }
        return hex.toString();
    }

    //将16进制数组转换为字符串
    public static String decode(String bytes) {
        String hexString="0123456789ABCDEF";
        ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
        //将每2位16进制整数组装成一个字节
//  for(int i=0;i<bytes.length();i+=2)
//      baos.write((hexString.indexOf(bytes.charAt(i))<<4 | hexString.indexOf(bytes.charAt(i+1))));
        //将每3位（第3位为空格）中的前2位16进制整数组装成一个字节
        for(int i=0;i<bytes.length();i+=3) {
            baos.write((hexString.indexOf(bytes.charAt(i))<<4 | hexString.indexOf(bytes.charAt(i+1))));
        }
        return new String(baos.toByteArray());
    }
    public interface OnPortListener{
        void onSend(String text);
    }
    public OnPortListener onPortListener;
    public void setOnPortListener(OnPortListener onPortListener){
        this.onPortListener = onPortListener;
    }
}
